package com.geely.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author : JCccc
 * @CreateTime : 2019/9/3
 * @Description :
 **/

@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";
    public final static String woman2 = "topic.woman2";

    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();

        connectionFactory.setPassword("guest");
        connectionFactory.setUsername("guest");
        connectionFactory.setVirtualHost("/");

        connectionFactory.setPublisherConfirms(true);

        //该方法配置多个host，在当前连接host down掉的时候会自动去重连后面的host
        connectionFactory.setAddresses("47.105.81.53:5672");
        return connectionFactory;
    }

    @Bean
    public Queue firstQueue() {
        return new Queue(TopicRabbitConfig.man);
    }


    //给队列topic.woman 设置死信队列
    @Bean
    public Queue secondQueue() throws Exception{
        //死信队列设置
        String dlxExchange = "dlx.exchange";
        String dlsQueue = "dlx.queue";
        String dlxRoutingKey = "topic.#";
        Map<String,Object> dtl =  new HashMap<>();
        //为队列设置队列交换器
        dtl.put("x-dead-letter-exchange", dlxExchange);
        //设置队列中的消息10s过期
        dtl.put("x-message-ttl", 20000);

        //创建死信隊列，和交换机互相綁定 这里注意必须是【相同的路由规则】
        //交换机类型fanout,topic,direct,handers
        Channel channel = connectionFactory().createConnection().createChannel(false);
        channel.exchangeDeclare(dlxExchange, "topic", true, false, null);
        channel.queueDeclare(dlsQueue, true, false, false, null);
        channel.queueBind(dlsQueue, dlxExchange, dlxRoutingKey);

        Map<String, Object> queueArgumentMap = new HashMap<>();
        queueArgumentMap.put("x-dead-letter-exchange", "monkey_exchange");
        return new Queue(TopicRabbitConfig.woman, true, false, false, dtl);
//        return new Queue(TopicRabbitConfig.woman);cj

    }

    @Bean
    public Queue thirdQueue() {
        return new Queue(TopicRabbitConfig.woman2);
    }

    //死信队列
    @Bean
    public Queue deadQueue() {
        return new Queue("monkey_queue");
    }

    @Bean
    TopicExchange deadExchange() {
        return new TopicExchange("monkey_exchange");
    }

    @Bean
    Binding bindingExchangeMessageDead(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("dead.#");
    }





    @Bean
    TopicExchange exchange() {
//        Map<String, Object> queueArgumentMap = new HashMap<>();
//        queueArgumentMap.put("x-message-ttl", "10000");
//        return new TopicExchange("topicExchange", true, false, queueArgumentMap);
        return new TopicExchange("topicExchange", true, false);
    }


    //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
    //这样只要是消息携带的路由键是topic.man,才会分发到该队列
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
    }

    //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
    // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
    @Bean
    Binding bindingExchangeMessage2() throws Exception {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
    }

    @Bean
    Binding bindingExchangeMessage3(){
        return BindingBuilder.bind(thirdQueue()).to(exchange()).with("topic.#");
    }


}
